<template>
  <div id="hAppTemplate" class="hAppTemplate">

    <!-- 新增/修改内容 -->
    <div class="modify_center">
      <el-form ref="addForm" :model="formDialog" :rules="rules" label-width="120px" label-position="right">

        <p class="title_H1" style="margin-bottom: 20px;">动态导航按钮</p>

        <div class="" style="padding: 0 80px;">
          <!-- 操作按钮 -->
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button icon="el-icon-refresh-left" size="mini" @click="getList">刷新</el-button>
            </el-col>
          </el-row>

          <!-- 表格数据 -->
          <el-table v-if="refreshTable" v-loading="loading" :header-cell-style="{background: $store.state.app.theme.tableBack,color: $store.state.app.theme.tableColor}" stripe border :data="businessList" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" @selection-change="handleSelectionChange" @cell-dblclick="celldblclick">
            <el-table-column label="按钮名称" align="center" prop="name" width="200px" :show-overflow-tooltip="true" />
            <el-table-column label="logo" align="center" prop="logo" width="100px">
              <template slot-scope="scope">
                <img :src="scope.row.logo" style="width: 40px" alt="">
              </template>
            </el-table-column>
            <el-table-column label="路由链接" align="center" prop="path" />
            <el-table-column label="排序" align="center" prop="sort" />
            <el-table-column label="按钮类别" align="center" prop="tag">
              <template slot-scope="scope">
                <div v-if="scope.row.tag == 'index'">首页</div>
                <div v-else-if="scope.row.tag == 'center'">个人中心</div>
                <div v-else>--</div>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="scope">
                <div v-if="scope.row.status == 0"><el-tag type="info">开发中</el-tag></div>
                <div v-else-if="scope.row.status == 1"><el-tag type="success">正常</el-tag></div>
                <div v-else>--</div>
              </template>
            </el-table-column>
            <!-- <el-table-column label="创建者" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" width="180px" prop="createTime" /> -->

            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button v-hasPermi="['system:template:edit']" size="mini" type="text" @click="handleUpdate(scope.row)">修改</el-button>
                <el-button v-if="scope.row.parentId != 0" v-hasPermi="['system:template:remove']" size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

      </el-form>
    </div>

    <!-- 操作按钮 -->
    <!-- <div class="button_center r_flex_center">
      <el-button type="primary" @click="submitForm">保存配置</el-button>
    </div> -->

    <!-- 对话框-修改 -->
    <el-dialog class="h5Template" :title="titleDialog === 'add' ? '新增APP导航按钮' : '修改APP导航按钮'" :visible.sync="openDialog" width="600px" append-to-body>
      <el-form ref="formDialog" :model="formDialog" :rules="rules" label-width="120px">

        <el-form-item label="按钮LOGO" prop="logo">
          <Up-img :oss-image-url="ossImageUrl" :theme="'rectangular'" :number="1" />
        </el-form-item>
        <el-form-item label="按钮名称" prop="name">
          <el-input v-model="formDialog.name" placeholder="请输入按钮名称" />
        </el-form-item>
        <el-form-item label="路由链接" prop="path">
          <el-input v-model="formDialog.path" placeholder="请输入路由链接" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="formDialog.sort" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="菜单状态" prop="status">
          <el-radio-group v-model="formDialog.status" placeholder="请选择" style="width: 100%">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">开发中</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单类别" prop="tag">
          <el-radio-group v-model="formDialog.tag" placeholder="请选择" style="width: 100%">
            <el-radio :label="'index'">首页</el-radio>
            <el-radio :label="'center'">个人中心</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoad" @click="submitForm">确 定</el-button>
        <el-button @click="openDialog = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listProduct, getProduct, addProduct, updateProduct } from '@/api/product/product'
import { getChannel, getCity } from '@/api/xxc'
import { cityTree } from '@/api/business/city'
import { guideList, delguide, getguide, upguide, addguide } from '@/api/business/system'
import UpImg from '@/components/UpImg'
import CityInput from '@/components/CityInput'
import MyCheckbox from '@/components/MyCheckbox'
import { checkPermi } from '@/utils/permission' // 权限判断函数

export default {
  name: 'HAppTemplate',
  components: { UpImg, CityInput, MyCheckbox },
  data() {
    return {
      // 所需材料
      materialAssets: [],
      // 上架星期
      weekArr: [],
      // 筛选项选中数组
      screeningArr: [],
      // 芝麻分选中数组
      zhimaCreditArr: [],
      microLoanArr: [],
      creditCardArr: [],
      peopleBankCreditArr: [],
      baitiaoArr: [],
      huabeiArr: [],
      // 社保选中数组
      fundArr: [],
      // 房产选中数组
      housePropertyArr: [],
      // 车产选中数组
      carPropertyArr: [],
      // 保单情况选中数组
      bdStatusArr: [],
      // 公积金缴纳选中数组
      hasArr: [],
      // 城市选中数组
      cityArr: [],
      // 城市选中数组-text
      cityArrHtml: '',
      // 全国城市数组
      allCityArr: [],
      // 选中省份
      cityKey: '',
      // 选中省份-全部城市
      cityActive: [],
      // 选中省份-选择城市
      cityActiveArr: [],
      // 渠道
      options: [],
      // 选中机构
      optionsKey: '',
      isShowTooltip: false,
      defaultTime: [],
      // 时间段
      alltime: '',
      // 上传图片选项
      ossImageUrl: [],
      // 选择城市-全选
      checkAll: false,
      // 选择城市-已经选择城市
      checkedCities: [],
      cities: ['房产', '车产', '公积金', '保险保单', '社保', '芝麻分', '身份证'],
      screlabelArr: [
        { name: '有公积金', id: 1 },
        { name: '有房产', id: 2 },
        { name: '有车产', id: 3 },
        { name: '有社保', id: 4 },
        { name: '有保险单', id: 5 }
      ],
      ArrCheck: [
        { name: '星期日', value: 0, active: false },
        { name: '星期一', value: 1, active: false },
        { name: '星期二', value: 2, active: false },
        { name: '星期三', value: 3, active: false },
        { name: '星期四', value: 4, active: false },
        { name: '星期五', value: 5, active: false },
        { name: '星期六', value: 6, active: false }
      ],
      isIndeterminate: true,
      // 遮罩层
      loading: true,
      // 修改渠道loading
      submitLoad: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      businessList: [],
      // 对话框类型
      titleDialog: 'add',
      // 是否显示对话框
      openDialog: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1, // 当前页数
        pageSize: 10, // 分页大小
        title: '', // 渠道名
        createBy: ''
      },
      // logo地址
      imageLogoUrl: '',
      imageLogoName: '',
      // 数据总条数
      totalPage: 0,
      // 表单参数
      value1: '',
      value2: '',
      // 新增、修改参数
      formDialog: {
        name: '',
        path: '',
        sort: '',
        status: 1,
        tag: '',
        logo: []
      },
      H5Img: [],
      // 表单选中的书籍
      multipleSelection: [],
      // 新增表单校验
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        path: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        tag: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        logo: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      },
      dialogVisible: false

    }
  },
  created() {
    this.getProduct()
    this.getList()
  },
  methods: {
    checkPermi,
    // 产品详情
    async getProduct() {
    },
    // 钩子 - 删除事件
    upimginfo(file) {
      console.log(file, 'file')
    },
    // 上传预处理
    beforeAvatarUpload(file) {
      if (file.type.indexOf('image/') === -1) {
        this.$modal.msgError('文件格式错误，请上传图片类型,如:JPG,PNG后缀的文件。')
      } else {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          this.imageLogoName = file
        }
        this.uploadImg(file)
      }
    },
    // 查询列表
    getList() {
      this.loading = true
      guideList(this.queryParams).then(response => {
        this.businessList = response.rows
        this.totalPage = response.total
        this.loading = false
      }).finally(() => { this.loading = false })
    },
    // 新增按钮操作
    handleAdd() {
      if (this.formDialog.id) {
        this.formDialog = this.$options.data().formDialog
        this.ossImageUrl = this.$options.data().ossImageUrl
      }
      this.openDialog = true
      this.titleDialog = 'add'
    },
    // 搜索按钮操作
    handleQuery() {
      // this.queryParams.pageNum = 1, // 当前页数
      this.getList()
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 删除按钮操作
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除')
        .then(function() {
          return delguide(row.id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.ossImageUrl = this.$options.data().ossImageUrl
      this.ossImageUrl = [{ id: '', url: row.logo }]
      // console.log(this.ossImageUrl)
      this.loading = true
      const upId = row.id
      getguide(upId).then(response => {
        this.loading = false
        this.openDialog = true
        this.titleDialog = 'up'
        this.formDialog = response.data
      }).finally(() => { this.loading = false })
    },
    // 修改按钮操作
    submitForm() {
      this.formDialog.logo = this.ossImageUrl.map(value => value.url)[0]
      // debugger
      this.$refs['formDialog'].validate(valid => {
        if (valid) {
          this.submitLoad = true
          if (this.titleDialog === 'add') {
            addguide(this.formDialog)
              .then(() => {
                this.submitLoad = false
                this.openDialog = false
                this.getList()
                this.$modal.msgSuccess('新增成功')
              })
              .catch(() => {
                this.submitLoad = false
              })
          } else {
            upguide(this.formDialog)
              .then(() => {
                this.submitLoad = false
                this.openDialog = false
                this.getList()
                this.$modal.msgSuccess('修改成功')
              })
              .catch(() => {
                this.submitLoad = false
              })
          }
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        parentId: '',
        deptName: '',
        pdAmount: '',
        tag: '',
        leader: '',
        type: '',
        url: '',
        zkStatus: '1'
      }
      this.resetForm('form')
    },
    // 分页请求
    handlePageChange(data) {
      this.queryParams.pageNum = data
      this.getList()
    },
    // 双击复制
    celldblclick(row, column, cell, event) {
      if (!column.property) return
      if (!row[column.property]) return
      const oInput = document.createElement('input')
      oInput.value = row[column.property]
      document.body.appendChild(oInput)
      oInput.select()
      document.execCommand('Copy')
      this.$message.success({
        message: '复制成功',
        showClose: true
      })
      oInput.remove()
    },
    // 选择表格
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(val)
    }
  }
}
</script>

<style scoped lang="scss">
.hAppTemplate {
  padding: 20px;
  .button_center{
    line-height: 40px;
  }
  .r_text_line{
    padding: 0 20px;
    overflow:hidden;
    text-overflow:ellipsis;
    display:-webkit-box;
    -webkit-box-orient:vertical;
    -webkit-line-clamp:2;
  }
  .el-input-group__append, .el-input-group__prepend{
    color: #918ef9 !important;
  }

}

.el-checkbox ::after {
  transition: none!important;
}
</style>

<style lang="css">
.hAppTemplate .el-input-group__append {
  color: #918ef9 !important;
}

.hAppTemplate .el-input.is-disabled .el-input__inner {
  color: #606266;
}
</style>
